/*
 * ============================================================================
 *
 *       Filename:  IPRecord.h
 *
 *    Description:  Declaration of IPRecord and Index
 *
 *         Author:  liangqing
 *
 * ============================================================================
 */
#ifndef IPRECORD_H
#define IPRECORD_H

#include <ostream>
#include <sstream>
#include <iomanip>

//#define USE_SET

using std::ostream;

class IPRecord {
  public:
    inline bool validate() { return (compare_ip(begin_ip, end_ip) <= 0); }

    static inline int compare_ip(const unsigned char *ip1, const unsigned char *ip2) {
        if(ip1[3] == ip2[3]){
            if(ip1[2] == ip2[2]){
                if(ip1[1] == ip2[1]){
                    return ip1[0] - ip2[0];
                }
                return ip1[1] - ip2[1];
            }
            return ip1[2] - ip2[2];
        }
        return ip1[3] - ip2[3];
    }

    unsigned char begin_ip[4];
    unsigned char end_ip[4];
    char region[256];
    char section[256];
};

struct Index{
    unsigned char ip[4];
#ifdef USE_SET
    unsigned char _ip[4];
#endif /* USE_SET */
    char offset[3];
    const static int size = 7;
};

ostream& operator<<(ostream& os, const IPRecord& record);

ostream& operator<<(ostream& os, const Index& index);

bool operator<(const Index &a, const Index &b);

#endif   /* #ifndef IPRECORD_H */


